import axios from "axios";
import { showToast, showLoadingToast, closeToast } from "vant";

// 创建axios实例
const request = axios.create({
  baseURL:"/app/",
  timeout: 20000
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    // 添加认证token
    const token = getToken();
    if (token) {
      config.headers.token = token;
    }

    // 显示加载提示（排除上传请求）
    if (!config.hideLoading && config.method !== "get") {
      showLoadingToast({
        message: "请求中...",
        forbidClick: true,
        duration: 0,
      });
    }

    return config;
  },
  (error) => {
    closeToast();
    showToast("请求配置错误");
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    closeToast();

    const { data } = response;

    // 根据后端返回的数据结构调整
    if (data.code !== undefined) {
      if (data.code === 200 || data.code === 0) {
        return data;
      } else if (data.code === 401) {
        // 未授权，清除token并跳转登录
        removeToken();
        showToast("登录已过期，请重新登录");
        // 可以在这里跳转到登录页面
        // router.push('/admin/login');
        return Promise.reject(new Error(data.message || "未授权"));
      } else {
        // 不在这里显示Toast，让组件自己处理
        // showToast(data.message || "请求失败");
        return data; // 返回原始数据，让组件判断
      }
    }

    // 如果后端直接返回数据
    return data;
  },
  (error) => {
    closeToast();

    let message = "网络错误";

    if (error.response) {
      const { status, data } = error.response;

      switch (status) {
        case 400:
          message = data.message || "请求参数错误";
          break;
        case 401:
          message = "未授权，请登录";
          removeToken();
          break;
        case 403:
          message = "权限不足";
          break;
        case 404:
          message = "请求的资源不存在";
          break;
        case 413:
          message = "上传文件过大";
          break;
        case 422:
          message = data.message || "数据验证失败";
          break;
        case 500:
          message = "服务器内部错误";
          break;
        case 502:
          message = "网关错误";
          break;
        case 503:
          message = "服务不可用";
          break;
        default:
          message = data.message || `请求失败 (${status})`;
      }
    } else if (error.request) {
      if (error.code === "ECONNABORTED") {
        message = "请求超时，请重试";
      } else {
        message = "网络连接失败";
      }
    }

    showToast(message);
    return Promise.reject(error);
  }
);

// Token管理
function getToken() {
  return (
    localStorage.getItem("vue_login_token") || sessionStorage.getItem("vue_login_token")
  );
}

function setToken(token, remember = false) {
  if (remember) {
    localStorage.setItem("vue_login_token", token);
  } else {
    sessionStorage.setItem("vue_login_token", token);
  }
}

function removeToken() {
  localStorage.removeItem("vue_login_token");
  sessionStorage.removeItem("vue_login_token");
}

// 文件上传专用请求方法
export function uploadRequest(url, formData, onProgress) {
  return request({
    url,
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
    timeout: 60000, // 上传文件超时时间更长
    hideLoading: true, // 不显示默认loading
    onUploadProgress: (progressEvent) => {
      if (onProgress) {
        const percentCompleted = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        );
        onProgress(percentCompleted);
      }
    },
  });
}

// 下载文件
export function downloadFile(url, filename) {
  return request({
    url,
    method: "get",
    responseType: "blob",
    hideLoading: true,
  }).then((response) => {
    const blob = new Blob([response.data]);
    const downloadUrl = window.URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = downloadUrl;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(downloadUrl);
  });
}

// 导出token管理方法
export { getToken, setToken, removeToken };

export default request;
